<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="160px" class="pms-form" size="mini" :inline="true">
    <el-row>
      <el-col v-for="(item, index) in formList" :key="index" :span="6">
        <el-form-item :required="item.value === 'endInternalAcceptDate'&& type === 'todo' &&(yanfa||softDev||serviceShow)" :label="item.value === 'endInternalAcceptDate'&&(yanfa||softDev||serviceShow)?'验收时间':item.label" :prop="item.value" style="width: 100%">
          <el-date-picker @change="changeAcceptDate" v-model="form[item.value]" :clearable="false" type="date" value-format="yyyy-MM-dd"
            :disabled="item.value === 'endInternalAcceptDate' && (type !== 'done'&&type !=='1')&&(yanfa||softDev||serviceShow) ? false : type !== 'todo' || !rules[item.alias ? item.alias : item.value]"
            placeholder="选择日期" :picker-options="{ disabledDate }" />
        </el-form-item>
      </el-col>
      <el-col :span="6" v-if="this.newPrjDetail.isNewBusiness==='1'&&showPayTime && !softDev || serviceShow">
        <el-form-item :label="'具备可起租条件'" style="width: 100%">
          <el-date-picker @change="changeDate" v-model="form['rentableOperateTime']" clearable type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
             :disabled="!(type==='todo'&& (this.newWorkDetail.bizObject.bizState==='F7' || serviceShow && this.newWorkDetail.bizObject.bizState==='F1'))"/>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getQueryObject } from '@/utils'
import { updateAcceptTimeApi} from '@/api/acceptance/index'
export default {
  name: 'Index',
  inject: ['prjDetail', 'workDetail'],
  props: {
    form: {
      type: Object,
      default() { return {} }
    },
    rules: {
      type: Object,
      default() { return {} }
    },
    formOptions: {
      type: Array,
      default() { return [] }
    },
    firstAcceptTime: {
      type: String,
      default() { return '' }
    },
    yanfa: {
      type: Boolean,
      default() { return false }
    },
      //是否是软件开发类
      softDev: {
          type: Boolean,
          default() { return false }
      },
      //是否是服务成本
      serviceShow: {
          type: Boolean,
          default() { return false }
      },
  },
  data() {
    return {
      formItem: [
        {
          label: '实际开工时间',
          value: 'realStartTime'
        },
        {
          label: '实际完工时间',
          value: 'realEndTime'
        },
        {
          label: '资源录入时间',
          value: 'resourceInTime'
        },
        {
          label: '资源确认时间',
          value: 'resourceConfirmTime'
        },
        {
          label: '内部验收时间',
          value: 'endInternalAcceptDate'
        },
        {
          label: '交付申请时间',
          value: 'jiaofuApplicationDate'
        },
        // {
        //   label: '移动设备安装完成时间',
        //   value: 'endYiDongInstallDate'
        // },
        // {
        //   label: '联通设备安装完成时间',
        //   value: 'endLianTongInstallDate'
        // },
        // {
        //   label: '电信设备安装完成时间',
        //   value: 'endDianXinInstallDate'
        // },
        // {
        //   label: '移动设备安装完成时间',
        //   value: 'endYiDongInstallDate',
        //   alias: 'endYiDongInstallDate2'
        // },
        // {
        //   label: '联通设备安装完成时间',
        //   value: 'endLianTongInstallDate',
        //   alias: 'endLianTongInstallDate2'
        // },
        // {
        //   label: '电信设备安装完成时间',
        //   value: 'endDianXinInstallDate',
        //   alias: 'endDianXinInstallDate2'
        // },
        {
          label: '交维结束时间',
          value: 'maintainDate'
        },
        {
          label: '具备可起租条件',
          value: 'rentableOperateTime'
        }
      ],
      type: ''
    }
  },
  computed: {
    formList() {
      return this.formItem.filter(item => this.formOptions.includes(item.alias ? item.alias : item.value))
    },
    newPrjDetail() {
      return this.prjDetail()
    },
    newWorkDetail() {
      return this.workDetail()
    },
    showPayTime() {
      const { bizObject } = this.newWorkDetail || {}
      const projectType = ['01', '03']
      if (projectType.includes(this.newPrjDetail.manageMode)) {
        return true
      }
      return false
    }
  },
  created() {
    const { type } = getQueryObject()
    this.type = type
  },
  methods: {
    changeAcceptDate(){
      window.sessionStorage.setItem('acceptTimeDate', this.form.endInternalAcceptDate)
    },
    // 保存起租时间
    async changeDate() {
        let params={}
        if(this.serviceShow){
            params = {
                prjId: this.newPrjDetail.prjId,
                rentableOperateTime: this.form.rentableOperateTime,
                endInternalAcceptDate: this.form.endInternalAcceptDate,
            }
        }else{
            params = {
                prjId: this.newPrjDetail.prjId,
                rentableOperateTime: this.form.rentableOperateTime,
            }
        }
        let saveTime = this.form.rentableOperateTime==null?'':this.form.rentableOperateTime
        window.sessionStorage.setItem('qizuTime', saveTime)
        const { code } = await updateAcceptTimeApi(params)
        if (+code === 200) {
        this.$message.success('保存成功')
        }
    },
    //如果不生效，把方法格式改为
    disabledDate(time) {
      const now = new Date(this.firstAcceptTime)
      let isNowDay = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0).getTime()
      return time.getTime() > Date.now() || time.getTime() < isNowDay
    },
    submitForm(fn) {
      return this.$refs.form.validate((valid) => {
        console.log('真的校验', valid)
        fn(valid)
      })
    }
  }
}
</script>
<style scoped lang='scss'>
.el-col {
  margin-bottom: 10px;
}
::v-deep .el-form-item__content .el-form-item__error{
  display: none;
}
</style>
